package org.cug.controller;

import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import org.cug.pojo.Article;
import org.cug.pojo.PageBean;
import org.cug.pojo.Result;
import org.cug.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    /**
     * @param article
     * @return
     * @author gongshihao
     * @state 已测验
     * @date 2024年5月15日
     */
    @PostMapping
    @Operation(summary = "新增文章数据")
    public Result add(@RequestBody @Validated Article article) {
        articleService.add(article);
        return Result.success();
    }

    /**
     * @param pageNum    当前页面
     * @param pageSize   页面数
     * @param categoryId 文章分类id
     * @param state      装填
     * @author gongshihao
     * @date 2024年6月5日11:32:27
     */
    @GetMapping
    public Result<PageBean<Article>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state
    ) {
        PageBean<Article> pb = articleService.list(pageNum, pageSize, categoryId, state);
        return Result.success(pb);
    }
}
